package com.scen.upload.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scen.upload.domain.Customer;
import com.scen.upload.domain.System;
import com.scen.upload.dto.CustomerDto;
import com.scen.upload.service.CustomerService;
import com.scen.upload.service.SystemService;
import com.scen.upload.shiro.ShiroSecurityUtils;
import com.scen.upload.utils.SelectView;
import com.scen.upload.vo.AjaxResult;
import com.scen.upload.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@RequestMapping("system/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SystemService systemService;

    /**
     * 分页查询客户
     *
     * @param customerDto
     * @return
     */
    @GetMapping("listCustomerForPage")
    public AjaxResult listCustomerForPage(CustomerDto customerDto) {
        DataGridView dataGridView = this.customerService.listCustomerPage(customerDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 根据客户id查询单个客户信息
     *
     * @param customerId
     * @return
     */
    @GetMapping("getCustomerById/{customerId}")
    public AjaxResult getCustomerById(@PathVariable @Validated @NotEmpty(message = "客户ID不能为空") Long customerId) {
        Customer customer = this.customerService.getOne(customerId);
        return AjaxResult.success(customer);
    }

    /**
     * 添加客户
     *
     * @param customerDto
     * @return
     */
    @PostMapping("addCustomer")
    public AjaxResult addCustomer(@Validated CustomerDto customerDto) {
        customerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        int i = this.customerService.addCustomer(customerDto);
        return AjaxResult.toAjax(i);
    }

    /**
     * 修改客户
     *
     * @param customerDto
     * @return
     */
    @PutMapping("updateCustomer")
    public AjaxResult updateCustomer(@Validated CustomerDto customerDto) {
        customerDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.customerService.updateCustomer(customerDto));
    }

    /**
     * 删除客户
     *
     * @param customerIds
     * @return
     */
    @DeleteMapping("deleteCustomerByIds/{customerIds}")
    public AjaxResult deleteCustomerByIds(@PathVariable @Validated @NotEmpty(message = "客户IDS不能为空") Long[] customerIds) {
        return AjaxResult.toAjax(this.customerService.deleteCustomerByIds(customerIds));
    }

    /**
     * 加载省份，部署方式下拉框
     *
     * @return
     */
    @GetMapping("loadDataSelect/{type}")
    public SelectView loadAddressSelect(@PathVariable @Validated String type) {
        QueryWrapper<System> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        List<System> list = this.systemService.list(queryWrapper);
        return new SelectView(list);
    }

    /**
     * 查询全部客户
     *
     * @return
     */
    @GetMapping("queryAllCustomer/{status}")
    public AjaxResult queryAllCustomer(@PathVariable @Validated String status) {
        return AjaxResult.success("查询成功", this.customerService.list(status));
    }

    /**
     * 根据省份数据得到对应id
     *
     * @param customerId
     * @return
     */
    @GetMapping("getAddressData/{customerId}")
    public AjaxResult getAddressData(@PathVariable @Validated String customerId) {
        return AjaxResult.success(this.customerService.getAddressData(customerId));
    }
}
